package edu.spec.wechat.controller;

import edu.spec.pms.model.dto.MvcReturn;
import edu.spec.pms.model.student.Student;
import edu.spec.pms.service.iface.IStudentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import javax.servlet.http.HttpSession;

/**
 * Created by MadFrog on 2016/11/6.
 */
@Controller
public class BindingController {

    @Inject
    private IStudentService studentService;

    @RequestMapping("/binding")
    public String binding(){
        return "student/binding";
    }

    @ResponseBody
    @RequestMapping(value = "/bindingStudent", method = RequestMethod.POST)
    public Object bindingStudent(@RequestParam String xh, @RequestParam String sfzh, HttpSession session){
        String message = "";
        String openId = (String) session.getAttribute("openId");
        Student stu = studentService.loadByXhAndSfzh(xh, sfzh);
        if(stu==null){
            message = "校验失败，请核对学号和身份证号！";
        } else {
            stu.setOpenId(openId);
            studentService.update(stu);
            session.setAttribute("user", stu);
            message = "绑定成功!";
        }
        return new MvcReturn(true, message);
    }

}
